package com.zhongke.cotrun.controller;

import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.pojo.ServiceRelease;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.model.validator.UserValidator;
import com.zhongke.cotrun.service.ServiceReleaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

@Validated
@RestController
@RequestMapping("/ServiceRelease")
public class ServiceReleaseController {
    @Autowired
    ServiceReleaseService serviceReleaseService;

    /**
     * 发布服务
     *
     * @param serviceRelease
     * @param session
     * @return
     * @throws MyException
     */
    @RequiresPermissions("web:serviceRelease:save")
    @PostMapping("/addServiceRelease")
    public APIResponse addServiceRelease(@Validated({UserValidator.InSet.class}) ServiceRelease serviceRelease, HttpSession session) throws MyException {

        if (serviceReleaseService.addServiceRelease(serviceRelease, session)) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.ADD_ERROR);
    }

    /**
     * 查看服务
     *
     * @param serviceRelease 搜索条件
     * @param pageNumber     第几页
     * @param pageSize       每页数
     * @return
     */
    @PostMapping("/selectServiceReleaseBycondent")
    public APIResponse selectServiceReleaseBycondent(ServiceRelease serviceRelease,
                                                     @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
                                                     @RequestParam(required = false, defaultValue = "12") Integer pageSize) {
        return APIResponse.returnSuccess(serviceReleaseService.selectServiceReleaseBycondition(serviceRelease, pageNumber, pageSize));
    }

    /**
     * 查看具体的
     * @param id
     * @return
     */
    @GetMapping("/selectServiceById")
    public APIResponse selectServiceById(@NotNull(message = "id不能为空") Integer id) throws MyException {
        return APIResponse.returnSuccess(serviceReleaseService.selectReleaseById(id));
    }

    /**
     * 跟新服务
     * @param serviceRelease
     * @return
     */
    @RequiresPermissions("web:serviceRelease:update")
    @PostMapping("/updateServiceRelease")
    public APIResponse updateServiceRelease(@Validated({UserValidator.UpDate.class}) ServiceRelease serviceRelease) {
        if (serviceReleaseService.updateServiceRelease(serviceRelease)) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.UPDATE_ERROR);
    }

    /**
     * 删除服务
     * @param serviceReleaseId
     * @return
     */
    @RequiresPermissions("web:serviceRelease:delete")
    @GetMapping("/delectServiceRelease")
    public  APIResponse delectServiceRelease(Integer serviceReleaseId ){
   if(serviceReleaseService.delectSerciceRelease(serviceReleaseId,2))  {
       return APIResponse.returnSuccess();
   }
   return  APIResponse.returnFail(CodeMsg.DELECT_ERROP);
    }
}
